//
//  ZCPictureView.swift
//  weibo
//
//  Created by 张城 on 16/7/26.
//  Copyright © 2016年 张城. All rights reserved.
//

import UIKit

class ZCPictureView: UICollectionView {
    
    let Margin: CGFloat = 10
    
    var images: [UIImage] = [UIImage]()
    
    
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func setUpUI() {
        dataSource = self
        delegate = self
        backgroundColor = UIColor.clearColor()
        
        let layout = self.collectionViewLayout as? UICollectionViewFlowLayout
        layout?.itemSize = CGSizeMake((SCREENW-40-2*Margin)/3, (SCREENW-40-2*Margin)/3)
        registerClass(ZCPictureCell.self, forCellWithReuseIdentifier: "cellId")
    }
    
    func addImage(image: UIImage){
        
        images.append(image)
        
        
        reloadData()
        
    }
    
    var closureImage:(()->())?
}

extension ZCPictureView: UICollectionViewDataSource,UICollectionViewDelegate {
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        if images.count == 0 || images.count == 0 {
            return images.count
        }else{
        return (images.count + 1)
        }
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cellId", forIndexPath: indexPath) as? ZCPictureCell
        
        if indexPath.item == images.count {
            
            cell?.image = nil
        }else{
            cell?.image = images[indexPath.item]
        }
        
        cell?.deleteImage = {[weak self] in
            
            self!.images.removeAtIndex(indexPath.item)
            
            self!.reloadData()
            
        }
        return cell!
    }
    
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        
        collectionView.deselectItemAtIndexPath(indexPath, animated: true)
        
        if indexPath.item == images.count {
            closureImage?()
        }
        
    }
}



class ZCPictureCell: UICollectionViewCell {
    
    var image: UIImage?{
        didSet{
            if image == nil {
                bgImgView.image = UIImage(named: "compose_pic_add")
                bgImgView.highlightedImage = UIImage(named: "compose_pic_add_highlighted")
                deleteBut.hidden = true
            }else{
            bgImgView.image = image
            bgImgView.highlightedImage = nil
            deleteBut.hidden = false
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = RandomColor()
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        addSubview(bgImgView)
        addSubview(deleteBut)
        
        bgImgView.snp_makeConstraints { (make) in
            make.edges.equalTo(0)
        }
        
        deleteBut.snp_makeConstraints { (make) in
            make.top.trailing.equalTo(contentView)
        }
        
    }
    
    private lazy var bgImgView:UIImageView = UIImageView()
    private lazy var deleteBut: UIButton = {
        let but = UIButton()
        but.setImage(UIImage(named:"compose_photo_close"), forState: .Normal)
        but.addTarget(self, action: #selector(ZCPictureCell.clickDeleteImage), forControlEvents: .TouchUpInside)
        return but
    }()
    
    var deleteImage:(()->())?
    func clickDeleteImage() {
        deleteImage!()
    }
    
}








